<template>
  <div>
    <el-upload
      class="upload-demo"
      :action="action"
      :headers="uploadHeaders"
      :before-upload="beforeUpload"
      :on-error="UpLoadError"
      :on-success="UpLoadSuccess"
      :on-remove="UpLoadRemove"
      multiple
      :limit="limit"
      :file-list="fileList"
    >
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">请上传最新的apk包,或wgt包</div>
    </el-upload>
  </div>
</template>

<script>
import { getAdminToken } from "@/utils/auth";
import { uploadURL } from "@/api/admin/common";

export default {
  name: "fileUp",
  props: {
    // 上传文件格式
    fileType: {
      type: String,
      default: "image",
    },
    // 最大上传个数
    limit: {
      type: Number,
      default: 9,
    },
    // 上传的文件格式
    accept: {
      type: String,
      default: "wgt,apk",
    },
    // 上传的文件格式
    maxsize: {
      type: Number,
      default: 102400,
    },
    multifile: {
      type: Boolean,
      default: false,
    },
    file: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      uploadHeaders: { token: getAdminToken() },
      action: uploadURL(), // 文件上传地址
      fileList: [],
      loading: "",
    };
  },
  watch: {
    file: function (val, oldval) {
      console.log(val, oldval, "123456");
      if (val) {
        if (val.url) {
          this.fileList = [
            {
              id: new Date().getTime(),
              name: val.url,
              url: val.url,
              path: val.url,
            },
          ];
        } else {
          this.fileList = [];
        }
      }
    },
  },
  methods: {
    // 文件上传之前拦截函数
    beforeUpload(file) {
      var num = file.size / 1024 / 1024;
      if (num > this.maxsize) {
        this.$messages.error("文件大小不能超过" + this.maxsize / 1024 + "M");
        return false;
      }
      var arrName = file.name.split(".");
      var arrName1 = this.accept.split(",");
      var format = false;
      arrName1.forEach((item) => {
        if (item == arrName[1]) {
          format = true;
        }
      });
      if (!format) {
        this.$messages.error("请上传" + this.accept + "格式的文件");
        return false;
      }
      this.loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
    },
    UpLoadError() {
      this.loading.close();
      this.$messages.error("文件上传失败");
    },
    UpLoadSuccess(res, file) {
      if (res.status == 1) {
        if (!this.multifile) {
          this.fileList = [
            {
              id: new Date().getTime(),
              name: res.data.name,
              url: res.data.full_filepath,
              path: res.data.filepath,
            },
          ];
        } else {
          this.fileList.push({
            id: new Date().getTime(),
            name: res.data.name,
            url: res.data.full_filepath,
            path: res.data.filepath,
          });
        }
      }
      this.loading.close();
      this.$messages.success("上传成功");
      if (!this.multifile) {
        this.$emit("uploadCallback", this.fileList[0]);
      } else {
        this.$emit("uploadCallback", this.fileList);
      }
    },
    UpLoadRemove(file, fileList) {
      var index = this.fileList.indexOf(file);
      if (index !== -1) {
        this.fileList.splice(index, 1);
      }
      if (!this.multifile) {
        this.$emit(
          "uploadCallback",
          this.fileList[0] ? this.fileList[0] : { url: "" }
        );
      } else {
        this.$emit("uploadCallback", this.fileList);
      }
    },
  },
};
</script>

<style>
</style>